package handler

import (
	"context"
	"goods_srv/global"
	"goods_srv/goodsPb"
	"goods_srv/model"

	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

func (s *GoodsService) CategoryBrandList(ctx context.Context, req *goods_pb.CategoryBrandFilterRequest) (*goods_pb.CategoryBrandListResponse, error) {
	var categoryBrands []model.GoodsCategoryBrand
	categoryBrandListResponse := goods_pb.CategoryBrandListResponse{}
	var total int64
	global.DB.Model(&model.GoodsCategoryBrand{}).Count(&total)

	categoryBrandListResponse.Total = int32(total)

	//Preload预加载
	global.DB.Model(&model.GoodsCategoryBrand{}).Preload("Category").Preload("Brand").Scopes(Paginate(int(req.Pages), int(req.PagePerNums))).Find(&categoryBrands)

	var categoryResponses []*goods_pb.CategoryBrandResponse

	for _, categoryBrand := range categoryBrands {
		categoryResponses = append(categoryResponses, &goods_pb.CategoryBrandResponse{
			Id: categoryBrand.BaseModel.ID,
			Category: &goods_pb.CategoryInfoResponse{
				Id:             categoryBrand.ID,
				Name:           categoryBrand.Category.Name,
				Level:          categoryBrand.Category.Level,
				IsTab:          categoryBrand.Category.IsTab,
				ParentCategory: categoryBrand.Category.ParentCategoryID,
			},
			Brand: &goods_pb.BrandInfoResponse{
				Id:   categoryBrand.Brand.ID,
				Name: categoryBrand.Brand.Name,
				Logo: categoryBrand.Brand.Logo,
			},
		})
	}
	categoryBrandListResponse.Data = categoryResponses
	return &categoryBrandListResponse, nil
}

// GetCategoryBrandList 根据分类获取品牌的信息
func (s *GoodsService) GetCategoryBrandList(ctx context.Context, req *goods_pb.CategoryInfoRequest) (*goods_pb.BrandListResponse, error) {

	brandListResponse := goods_pb.BrandListResponse{}

	var category model.Category

	//检测分类是否存在
	if result := global.DB.Find(&category, req.Id).First(&category); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "品牌分类不存在")
	}

	//定义的是分类品牌的信息
	var categoryBrands []model.GoodsCategoryBrand

	if result := global.DB.Where(&model.GoodsCategoryBrand{CategoryID: category.ID}).Preload("Brand").Find(&categoryBrands); result.RowsAffected > 0 {
		brandListResponse.Total = int32(result.RowsAffected)
	}

	//定义品牌信息
	var brandInfoResponses []*goods_pb.BrandInfoResponse

	for _, categoryBrand := range categoryBrands {
		brandInfoResponses = append(brandInfoResponses, &goods_pb.BrandInfoResponse{
			Id:   categoryBrand.Brand.ID,
			Name: categoryBrand.Brand.Name,
			Logo: categoryBrand.Brand.Logo,
		})
	}
	brandListResponse.Data = brandInfoResponses
	return &brandListResponse, nil
}

// CreateCategoryBrand 创建分类额品牌
func (s *GoodsService) CreateCategoryBrand(ctx context.Context, req *goods_pb.CategoryBrandRequest) (*goods_pb.CategoryBrandResponse, error) {

	var category model.Category
	if result := global.DB.First(&category, req.CategoryId); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "商品分类不存在")
	}
	var brand model.Brands
	if result := global.DB.First(&brand, req.BrandId); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "品牌不存在")
	}
	categoryBrand := model.GoodsCategoryBrand{
		CategoryID: req.CategoryId,
		BrandsID:   req.BrandId,
	}
	tx := global.DB.Save(&categoryBrand)
	if tx.Error != nil {
		return nil, status.Errorf(codes.Internal, "创建分类品牌失败")
	}
	return &goods_pb.CategoryBrandResponse{Id: categoryBrand.ID}, nil
}

func (s *GoodsService) DeleteCategoryBrand(ctx context.Context, req *goods_pb.CategoryBrandRequest) (*goods_pb.Empty, error) {
	if result := global.DB.Delete(&model.GoodsCategoryBrand{}, req.Id); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "品牌分类不存在")
	}
	return &goods_pb.Empty{}, nil
}

func (s *GoodsService) UpdateCategoryBrand(ctx context.Context, req *goods_pb.CategoryBrandRequest) (*goods_pb.Empty, error) {
	var categoryBrand model.GoodsCategoryBrand
	if result := global.DB.First(&categoryBrand, req.Id); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "品牌分类不存在")
	}
	var category model.Category
	if result := global.DB.First(&category, req.CategoryId); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "商品分类不存在")
	}
	var brand model.Brands
	if result := global.DB.First(&brand, req.BrandId); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.InvalidArgument, "品牌不存在")
	}
	categoryBrand.CategoryID = req.CategoryId
	categoryBrand.BrandsID = req.BrandId
	global.DB.Save(&categoryBrand)
	return &goods_pb.Empty{}, nil
}
